//
//  XYAppDelegate.m
//  XYLib
//
//  Created by XY on 2021/6/9.
//

#import "XYAppDelegate.h"
#import <WeexSDK/WeexSDK.h>
#import <NSString+CODEC.h>
#import "WXImgLoaderDefaultImpl.h"
#import "WXConfigCenterDefaultImpl.h"
#import "WXNavigationHandlerImpl.h"
#import "WXApmGeneratorImpl.h"
#import "WXWebSocketDefaultImpl.h"
#import "Reachability.h"

@interface XYAppDelegate()
@property (nonatomic, copy) NSString* appNetWork;
@property (nonatomic) Reachability *hostReachability;
@end

@implementation XYAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self initXYLib];
    
    [self setNetWork];
    
    return YES;
}

-(void)setNetWork
{
    /*
     Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the method reachabilityChanged will be called.
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    //Change the host name here to change the server you want to monitor.
    self.hostReachability = [Reachability reachabilityWithHostName:@"www.apple.com"];
    [self.hostReachability startNotifier];
    [self updateInterfaceWithReachability:self.hostReachability];
}

/*!
 * Called by Reachability whenever status changes.
 */
- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    [self updateInterfaceWithReachability:curReach];
}
- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
    NSString *status = @"NONE";
    if (![NSString isBlankString:self.appNetWork]) {
        status = self.appNetWork;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:status forKey:@"oldStatus"];
    if (reachability == self.hostReachability){
        NetworkStatus netStatus = [reachability currentReachabilityStatus];
        switch (netStatus)
        {
            case NotReachable:        {
                status = @"NONE";
                break;
            }
            case ReachableViaWWAN:    {
                status = @"WWAN";
                break;
            }
            case ReachableViaWiFi:    {
                status = @"WIFI";
                break;
            }
        }
        self.appNetWork = status;
        [dic setObject:self.appNetWork forKey:@"netStatus"];
        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"netWorkStatus"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        WXBaseViewController* controller =  [[(WXRootViewController*)self.window.rootViewController viewControllers] lastObject];
        if ([controller respondsToSelector:@selector(fireGlobalEvent:params:)]) {
            [controller fireGlobalEvent:@"netWorkStatus" params:dic];
        }
    }
}

- (void)initXYLib
{
    [WXSDKEngine initSDKEnvironment];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        [WXSDKEngine setCustomEnvironment:@{@"deviceName":@"iPad"}];
    }else{
        [WXSDKEngine setCustomEnvironment:@{@"deviceName":@"iPhone"}];
    }
    [WXSDKEngine setCustomEnvironment:@{@"bundleId":[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]}];
    
    [WXSDKEngine registerHandler:[WXImgLoaderDefaultImpl new] withProtocol:@protocol(WXImgLoaderProtocol)];
    [WXSDKEngine registerHandler:[WXConfigCenterDefaultImpl new] withProtocol:@protocol(WXConfigCenterProtocol)];
    [WXSDKEngine registerHandler:[WXNavigationHandlerImpl new] withProtocol:@protocol(WXNavigationProtocol)];
    [WXSDKEngine registerHandler:[WXApmGeneratorImpl new] withProtocol:@protocol(WXApmGeneratorProtocol)];
    [WXSDKEngine registerHandler:[WXWebSocketDefaultImpl new] withProtocol:@protocol(WXWebSocketHandler)];
    
    [WXSDKEngine registerComponent:@"select" withClass:NSClassFromString(@"WXSelectComponent")];
    [WXSDKEngine registerComponent:@"media" withClass:NSClassFromString(@"MediaComponent")];
    [WXSDKEngine registerComponent:@"mediaPlayer" withClass:NSClassFromString(@"MediaComponent")];
    [WXSDKEngine registerComponent:@"fullChart" withClass:NSClassFromString(@"BIBFullChartComponent")];
    [WXSDKEngine registerComponent:@"quoteKLineChart" withClass:NSClassFromString(@"BIBKlineChartComponent")];
    [WXSDKEngine registerComponent:@"proviewImg" withClass:NSClassFromString(@"PreviewImageComponent")];
    [WXSDKEngine registerComponent:@"camera" withClass:NSClassFromString(@"CameraComponent")];
    [WXSDKEngine registerComponent:@"chatBox" withClass:NSClassFromString(@"ChatBoxComponent")];
    
    [WXSDKEngine registerModule:@"app" withClass:NSClassFromString(@"WXApplicationModule")];
    [WXSDKEngine registerModule:@"cookieStorage" withClass:NSClassFromString(@"CookieStorageModule")];
    [WXSDKEngine registerModule:@"tripleDes" withClass:NSClassFromString(@"TripleDESModule")];
    [WXSDKEngine registerModule:@"jpush" withClass:NSClassFromString(@"JPushModule")];
    [WXSDKEngine registerModule:@"mediaplayer" withClass:NSClassFromString(@"MediaPlayerModule")];
    [WXSDKEngine registerModule:@"QRCodeModule" withClass: NSClassFromString(@"QRCodeModule")];
    [WXSDKEngine registerModule:@"imagePicker" withClass:NSClassFromString(@"WXImagePickerModule")];
    [WXSDKEngine registerModule:@"transfer" withClass:NSClassFromString(@"WXTransferModule")];
    [WXSDKEngine registerModule:@"fileManager" withClass:NSClassFromString(@"FileManagerModule")];
    [WXSDKEngine registerModule:@"downloadManager" withClass:NSClassFromString(@"DownloadModule")];
    [WXSDKEngine registerModule:@"splash" withClass:NSClassFromString(@"SplashScreen")];
    
    //    [WPRegister registerPlugins];
    
#ifdef DEBUG
    [WXDebugTool setDebug:YES];
    [WXLog setLogLevel:WXLogLevelLog];
#else
    [WXDebugTool setDebug:NO];
    [WXLog setLogLevel:WXLogLevelError];
#endif
}
@end
